﻿using DB.Models;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebApi_For_Auth.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ThirdPartyEntranceController : ControllerBase
    {
        // GET: api/<ThirdPartyEntranceController>
        [HttpGet]
        public List<ThirdPartyEntrance> Get()
        {
            using(DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var data = db.ThirdPartyEntrances.ToList();
                return data;
            }
        }

        // GET api/<ThirdPartyEntranceController>/5
        [HttpGet("{id}")]
        public ThirdPartyEntrance Get(string id)
        {
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var data = db.ThirdPartyEntrances.Where(o => o.Id == id).SingleOrDefault();
                return data;
            }
        }

        // POST api/<ThirdPartyEntranceController>
        [HttpPost]
        public void Post([FromBody] ThirdPartyEntrance value)
        {
            if (value.Id == null)
            {
                value.Id = Guid.NewGuid().ToString();
            }
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                db.ThirdPartyEntrances.Add(value);
                db.SaveChanges();
            }
        }

        // PUT api/<ThirdPartyEntranceController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] ThirdPartyEntrance value)
        {
            if (value.Id == null)
            {
                value.Id = Guid.NewGuid().ToString();
            }
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var entrance = db.ThirdPartyEntrances.Where(o => o.Id == value.Id).SingleOrDefault();
                if (entrance != null)
                {
                    entrance.Key = value.Key;
                    entrance.BackValue = value.BackValue;
                    entrance.UpdateTime = value.UpdateTime;
                    db.SaveChanges();
                }
            }
        }

        // DELETE api/<ThirdPartyEntranceController>/5
        [HttpDelete("{id}")]
        public void Delete(ThirdPartyEntrance id)
        {
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var entrance = db.ThirdPartyEntrances.Where(o => o.Id == id.Id).SingleOrDefault();
                if (entrance != null)
                {
                    db.ThirdPartyEntrances.Remove(entrance);
                    db.SaveChanges();
                }
            }
        }
    }
}
